package cn.lecosa.es;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class EsClient {

	public static TransportClient eSClient() {
		// 9300端口： ES节点之间通讯使用
		// 9200端口： ES节点 和 外部 通讯使用
		// （9300是tcp通讯端口，集群间和TCPClient都走的它；9200是http协议的RESTful接口）
		TransportAddress address= null;
		try {
			address = new TransportAddress(
					InetAddress.getByName("park01"), 9300);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		Settings settings = Settings.builder()
				.put("cluster.name", "my-application") .put("client.transport.ignore_cluster_name", true) .build(); // cluster.name: my-application  yml文件里面有
		TransportClient client = new PreBuiltTransportClient(settings);
		client.addTransportAddress(address);
		return client;
	}
	public static void main(String[] args) {
		System.out.println(EsClient.eSClient());
	}
}
